//
//  YSAccountCenterItemView.m
//  Yueshi
//
//  Created by Northwan on 15/7/30.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSAccountCenterItemView.h"

@interface YSAccountCenterItemView()
@property (strong, nonatomic) UIImageView *iconView;
@property (strong, nonatomic) UILabel *nameLabel;
@property (strong, nonatomic) UILabel *colorLabel;
@property (strong, nonatomic) UILabel *priceLabel;
@property (strong, nonatomic) UILabel *numLabel;
@end
@implementation YSAccountCenterItemView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //商品图片
        UIImageView *iconView = [[UIImageView alloc]init];
        iconView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:iconView];
        self.iconView = iconView;
        
        //名字
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.numberOfLines = 1;
        nameLabel.textColor = NAME_COLOR;
        nameLabel.font = SYS_FONT(NAME_FONT);
        [nameLabel setNumberOfLines:0];
        [self addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        //颜色分类
        UILabel *colorLabel = [[UILabel alloc] init];
        colorLabel.textColor = SUB_COLOR;
        colorLabel.font = SYS_FONT(SUB_FONT);
        [self addSubview:colorLabel];
        self.colorLabel = colorLabel;
        
        //现价
        UILabel *priceLabel = [[UILabel alloc] init];
        priceLabel.textColor = ThemeColor;
        priceLabel.font = SYS_FONT(NEWPRICE_FONT);
        [self addSubview:priceLabel];
        self.priceLabel = priceLabel;
        
        //数量
         UILabel *numLabel = [[UILabel alloc] init];
         numLabel.textColor = NUM_COLOR;
         numLabel.font = SYS_FONT(NUM_FONT);
         [self addSubview:numLabel];
         self.numLabel = numLabel;
        
    }
    return self;
}

-(void)setAccountItemFrame:(YSAccountCenterItemFrame *)accountItemFrame{
    _accountItemFrame = accountItemFrame;
    
    YSCartProduct *product = accountItemFrame.product;
    
    [self.iconView setImageURLStr:product.prodImageUrl];
    self.iconView.frame = accountItemFrame.iconFrame;
    
    if(product.isPresent){//判断是否是赠品
        NSString *prodName = [NSString stringWithFormat:@"(赠)%@",product.prodName];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:prodName];
        [str addAttribute:NSForegroundColorAttributeName value:ThemeColor range:NSMakeRange(0,3)];
        NSString *prodPriceText = [NSString stringWithFormat:@"¥%.1f",product.prodNewPrice];
        [self.priceLabel setText:prodPriceText];
        self.priceLabel.frame = accountItemFrame.newPriceFrame;
        [self.nameLabel setAttributedText:str];
        self.nameLabel.frame = accountItemFrame.nameFrame;
    }else if(product.prodPromotion && product.prodPromotion.price){//判断抢购
         NSString *prodPriceText = [NSString stringWithFormat:@"¥%.1f",product.prodPromotion.price];
        if(product.prodPromotion.isPanicBuy){
            NSString *prodName = [NSString stringWithFormat:@"(抢)%@",product.prodName];
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:prodName];
            [str addAttribute:NSForegroundColorAttributeName value:ThemeColor range:NSMakeRange(0,3)];
            [self.nameLabel setAttributedText:str];
        }
        self.nameLabel.frame = accountItemFrame.nameFrame;
        [self.priceLabel setText:prodPriceText];
        self.priceLabel.frame = accountItemFrame.newPriceFrame;
    }else{
        [self.nameLabel setText:product.prodName];
        self.nameLabel.frame = accountItemFrame.nameFrame;
        [self.priceLabel setText:[NSString stringWithFormat:@"¥%.1f",product.prodNewPrice]];
        self.priceLabel.frame = accountItemFrame.newPriceFrame;
    }
    
    [self.colorLabel setText:product.prodBuyLink];
    self.colorLabel.frame = accountItemFrame.colorFrame;
    
    [self.numLabel setText:[NSString stringWithFormat:@"x%ld",product.prodCount]];
     self.numLabel.frame = accountItemFrame.numFrame;
    
    self.cellHeight = accountItemFrame.cellHeight;
    self.frame = accountItemFrame.frame;
}

@end
